home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / EXAMPLES / DIALS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  3.1 KB  |  146 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1994. */
  3.  
  4. /* This program is freely distributable without licensing fees 
  5.    and is provided without guarantee or warrantee expressed or 
  6.    implied. This program is -not- in the public domain. */
  7.  
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <math.h>
  12. #include <GL/glut.h>
  13.  
  14. int *dials, *buttons;
  15. int numdials, numbuttons;
  16. int dw, bw;
  17.  
  18. /* Some <math.h> files do not define M_PI... */
  19. #ifndef M_PI
  20. #define M_PI 3.14159265358979323846
  21. #endif
  22.  
  23. void
  24. drawCircle(int x, int y, int r, int dir)
  25. {
  26.    float angle;
  27.  
  28.    glPushMatrix();
  29.    glTranslatef(x,y,0);
  30.    glBegin(GL_TRIANGLE_FAN);
  31.    glVertex2f(0,0);
  32.    for(angle = 2*M_PI; angle >= 0; angle -= M_PI/12) {
  33.       glVertex2f(r*cos(angle),r*sin(angle));
  34.    }
  35.    glEnd();
  36.    glColor3f(0,0,1);
  37.    glBegin(GL_LINES);
  38.    glVertex2f(0,0);
  39.    glVertex2f(r*cos(dir*M_PI/180),r*sin(dir*M_PI/180));
  40.    glEnd();
  41.    glPopMatrix();
  42. }
  43.  
  44. void
  45. displayDials(void)
  46. {
  47.   int i;
  48.  
  49.   glClear(GL_COLOR_BUFFER_BIT);
  50.   for(i=0;i<numdials;i++) {
  51.     glColor3f(0, 1, 0);
  52.     drawCircle(100 + (i%2) * 100, 100 + (i/2) * 100, 40, -dials[i]+90);
  53.   }
  54.   glutSwapBuffers();
  55. }
  56.  
  57. void
  58. displayButtons(void)
  59. {
  60.   int i, n;
  61.  
  62.   glClear(GL_COLOR_BUFFER_BIT);
  63.   glBegin(GL_QUADS);
  64.   for(i=0,n=0;i<32;i++,n++) {
  65.     switch(n) {
  66.     case 0:
  67.     case 5:
  68.     case 30:
  69.       n++;
  70.     }
  71.     if(buttons[i]) {
  72.       glColor3f(1,0,0);
  73.     } else {
  74.       glColor3f(1,1,1);
  75.     }
  76.     glVertex2f((n%6)*40+10,(n/6)*40+10);
  77.     glVertex2f((n%6)*40+30,(n/6)*40+10);
  78.     glVertex2f((n%6)*40+30,(n/6)*40+30);
  79.     glVertex2f((n%6)*40+10,(n/6)*40+30);
  80.   }
  81.   glEnd();
  82.   glutSwapBuffers();
  83. }
  84.  
  85. void
  86. reshape(int w, int h)
  87. {
  88.   glViewport(0, 0, w, h);
  89.   glMatrixMode(GL_PROJECTION);
  90.   glLoadIdentity();
  91.   gluOrtho2D(0, w, 0, h);
  92.   if(glutGetWindow() == bw) {
  93.      glScalef(1, -1, 1);
  94.      glTranslatef(0, -h, 0);
  95.   }
  96.   glMatrixMode(GL_MODELVIEW);
  97. }
  98.  
  99. void
  100. dodial(int dial, int value)
  101. {
  102.   dials[dial - 1] = value % 360;
  103.   glutPostWindowRedisplay(dw);
  104. }
  105.  
  106. void
  107. dobutton(int button, int state)
  108. {
  109.   if(button <= numbuttons) {
  110.     buttons[button-1] = (state == GLUT_DOWN);
  111.     glutPostWindowRedisplay(bw);
  112.   }
  113. }
  114.  
  115. int
  116. main(int argc, char **argv)
  117. {
  118.   glutInit(&argc, argv);
  119.   numdials = glutDeviceGet(GLUT_NUM_DIALS);
  120.   if(numdials <= 0) {
  121.      fprintf(stderr, "dials: No dials available\n");
  122.      exit(1);
  123.   }
  124.   numbuttons = glutDeviceGet(GLUT_NUM_BUTTON_BOX_BUTTONS);
  125.   dials = (int*) calloc(numdials, sizeof(int));
  126.   buttons = (int*) calloc(numbuttons, sizeof(int));
  127.   glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  128.   glutInitWindowSize(300, ((numdials+1)/2) * 100 + 100);
  129.   dw = glutCreateWindow("GLUT dials");
  130.   glClearColor(0.5, 0.5, 0.5, 1.0);
  131.   glLineWidth(3.0);
  132.   glutDialsFunc(dodial);
  133.   glutButtonBoxFunc(dobutton);
  134.   glutDisplayFunc(displayDials);
  135.   glutReshapeFunc(reshape);
  136.   glutInitWindowSize(240, 240);
  137.   bw = glutCreateWindow("GLUT button box");
  138.   glClearColor(0.5, 0.5, 0.5, 1.0);
  139.   glutDisplayFunc(displayButtons);
  140.   glutReshapeFunc(reshape);
  141.   glutDialsFunc(dodial);
  142.   glutButtonBoxFunc(dobutton);
  143.   glutMainLoop();
  144.   return 0;             /* ANSI C requires main to return int. */
  145. }
  146.